"""
数据集合法性验证
"""
import os
import cv2
import config


# 模型全局变量
REVISE_WIDTH = config.REVISE_WIDTH
REVISE_HEIGHT = config.REVISE_HEIGHT
# 路径全局变量
INPUT_VIDEO = 'E:/Trainsets_LSTM/'  # 输入视频流
OUTPUT_VIDEO = 'E:/Datasets/'   # 输出视频流


if __name__ == '__main__':

    file_list = os.listdir(INPUT_VIDEO)  # 获取视频列表
    # 遍历所有视频文件
    for file in range(len(file_list)):
        file_input = os.path.join(INPUT_VIDEO, str(file_list[file]))
        video_list = os.listdir(file_input)
        # 若不存在目录，新建之
        file_output = os.path.join(OUTPUT_VIDEO, str(file_list[file]))
        isExists = os.path.exists(file_output)
        if not isExists:
            os.makedirs(file_output)
        # 读取视频
        for video in range(len(video_list)):
            video_path = os.path.join(INPUT_VIDEO, str(file_list[file]), str(video_list[video]))
            video_capture = cv2.VideoCapture(video_path)
            out = cv2.VideoWriter(os.path.join(file_output, str(video) + '.mp4'), cv2.VideoWriter_fourcc(*'XVID'), 30.0, (480, 640), True)   # 输出演示视频
            print(os.path.join(file_output, str(video) + '.mp4'))
            while True:
                ret, frame = video_capture.read()
                # 判断视频流中的帧是否存在
                if frame is None:
                    break
                # 强制竖屏
                if frame.shape[1] > frame.shape[0]:
                    frame = cv2.transpose(frame)
                    frame = cv2.flip(frame, 1)
                frame = cv2.resize(frame, (REVISE_WIDTH, REVISE_HEIGHT))
                cv2.imshow('show_img', frame)
                out.write(frame)    # 写入视频流
                if cv2.waitKey(1) & 0xff == ord('n'):
                    continue
    cv2.destroyAllWindows()
